package com.edfu.core.commons.service.ws;

import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.edfu.core.commons.beans.Category;
import com.edfu.core.commons.service.api.ICategoryMgtService;

@Path("/")
@Produces( { MediaType.APPLICATION_JSON })
@Consumes(MediaType.APPLICATION_XML)
@WebService(targetNamespace = "http://com.edfu.core.commons.service.ws.CategoryMgtService")
public class CategoryMgtServiceDelegate {
	
	private static Logger logger = LoggerFactory.getLogger(CategoryMgtServiceDelegate.class);
	
	@Autowired
	private ICategoryMgtService categoryMgtService;
	
	@POST
	@Path("/categories")
	@WebMethod(operationName = "createCategory")
	public Response createCategory(Category category) {
		Category tc = categoryMgtService.createCategory(category);
		logger.debug("Category is created successfully");
		
		return Response.ok(tc).build();
	}

	@PUT
	@Path("/categories")
	@WebMethod(operationName = "updateCategory")
	public Response updateCategory(Category category) {
		Category tc = categoryMgtService.updateCategory(category);
		logger.debug("Category is updated successfully");
		
		return Response.ok(tc).build();
	}

	@DELETE
	@Path("/categories/{categoryId}")
	@WebMethod(operationName = "removeCategory")
	public Response removeCategory(@PathParam(value = "categoryId") Integer categoryId) {
		categoryMgtService.removeCategory(categoryId);
		logger.debug("Category is removed successfully");
		
		return Response.ok().build();
	}

	@GET
	@Path("/categories")
	@WebMethod(operationName = "getCategories")
	public List<Category> getCategories() {
		return categoryMgtService.getCategories();
	}

	@GET
	@Path("/categories/{categoryId}")
	@WebMethod(operationName = "getCategory")
	public Category getCategory(@PathParam(value = "categoryId") Integer categoryId) {
		return categoryMgtService.getCategory(categoryId);
	}
	
	@POST
	@Path("/categories/add")
	@WebMethod(operationName = "addCategories")
	public Response addCategories(Category category) {
		List<Category> tcList = categoryMgtService.addCategories(category.getParentCategory().getName(), category.getChildCategories());
		logger.debug("Categories are crated successfully");
		
		return Response.ok().build();
	}

}
